package com.example.toutiao.ui.main.act

import android.os.Build
import androidx.annotation.RequiresApi
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import androidx.fragment.app.Fragment
import com.example.toutiao.R
import com.example.toutiao.base.BaseActivity
import com.example.toutiao.ui.main.adpater.HomeAdapter
import com.example.toutiao.ui.main.frg.HomeFragment
import com.example.toutiao.ui.main.frg.MinVideoFragment
import com.example.toutiao.ui.main.frg.MineFragment
import com.example.toutiao.ui.main.frg.VideoFragment
import com.example.toutiao.ui.main.model.MainModel
import com.example.toutiao.ui.main.model.TitleModel
import com.example.toutiao.ui.main.presenter.MainPresenter
import com.example.toutiao.ui.main.view.MainView
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : BaseActivity<MainView, MainPresenter>(), MainView {

    private val titleTabs = ArrayList<CustomTabEntity>()
    private val fragments = ArrayList<Fragment>()

    override fun getLayoutId(): Int = R.layout.activity_main
    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    override fun initView() {
        val wic = ViewCompat.getWindowInsetsController(window.decorView)
        wic?.isAppearanceLightStatusBars = false
        window.statusBarColor = ContextCompat.getColor(this@MainActivity,R.color.c_E6645F)
        val titles = resources.getStringArray(R.array.title)
        val selectIds = resources.obtainTypedArray(R.array.select)
        val unSelectIds = resources.obtainTypedArray(R.array.unselect)
        for (i: Int in titles.indices) {
            titleTabs.add(
                TitleModel(
                    titles[i],
                    selectIds.getResourceId(i, 0),
                    unSelectIds.getResourceId(i, 0),
                )
            )
        }
        fragments.add(HomeFragment())
        fragments.add(VideoFragment())
        fragments.add(MinVideoFragment())
        fragments.add(MineFragment())
        vp_home.adapter=HomeAdapter(supportFragmentManager, fragments)
        vp_home.offscreenPageLimit=fragments.size
        ctl_home.setTabData(titleTabs)

        /* 监听tab点击 */
        ctl_home.setOnTabSelectListener(object : OnTabSelectListener {
            @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
            override fun onTabSelect(position: Int) {
                vp_home.setCurrentItem(position, false)


                when(position){
                    0,1,2->{
                        wic?.isAppearanceLightStatusBars = false
                        window.statusBarColor = ContextCompat.getColor(this@MainActivity,R.color.c_E6645F)
                    }
                    3->{
                        wic?.isAppearanceLightStatusBars = true
                        window.statusBarColor = ContextCompat.getColor(this@MainActivity,R.color.white)
                    }
                }

            }

            override fun onTabReselect(position: Int) {

            }

        })
    }


    override fun initData() {

    }
    override fun <T> setData(data: T) {
        val mainModel=data as MainModel

    }

    override fun setError(err: String) {

    }

    override fun createPresenter(): MainPresenter =MainPresenter()

    override fun isRegister(): Boolean =false
}


